<%@ page language="java" contentType="text/html; charset=US-ASCII"
	pageEncoding="US-ASCII"%>
<%@ page import="java.sql.*"%>
<%@ page import="ca.radiologydb.util.Template"%>
<%@ page import="ca.radiologydb.util.ConnectionManager" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Record & Image Upload</title>
<%
	// @author Addison Revoy
	Template template = new Template(this.getServletContext()
			.getRealPath("/").toString());
	out.println(template.getTemplateTop());
%>
</head>
<body>

	<%
		if (session.getAttribute("username") == null
				|| !session.getAttribute("usertype").equals("r")) {
			out.println("Not logged in/Don't have permission to access this resource.<br>");
			out.println("<a href='login.jsp'>Login</a>");
		} else {
			//establish the connection to the underlying database
			ConnectionManager manager = new ConnectionManager();
			Connection conn = manager.getConnection();
			out.println("<h3>Radiology Record Uploader.</h3>");

			Statement stmt = null;
			ResultSet rset = null;

			if (request.getParameter("bSubmit") != null) {
				//From record upload to picture upload

				int record_id;

				String sql = "select max(record_id) from radiology_record";

				try {
					stmt = conn.createStatement();
					rset = stmt.executeQuery(sql);
				} catch (Exception ex) {
					out.println("<hr>" + ex.getMessage() + "<hr>");
				}

				if (rset.next())
					record_id = rset.getInt(1) + 1;
				else
					record_id = 0;

				rset.close();

				String user_name = (String) session
						.getAttribute("user_name");

				String patient = request.getParameter("patientName").trim();
				String doctor = request.getParameter("doctorName").trim();
				//String radiologist = request.getParameter("radName").trim();
				String radiologist = (String) session
						.getAttribute("username");
				String testType = request.getParameter("test").trim();
				String preDate = request.getParameter("pdate").trim();
				String testDate = request.getParameter("tdate").trim();
				String diagnosis = request.getParameter("diagnosis").trim();
				String description = request.getParameter("description")
						.trim();

				sql = "INSERT INTO radiology_record VALUES(" + record_id
						+ ",'" + patient + "','" + doctor + "','"
						+ radiologist + "','" + testType + "',";
				sql += "to_date('" + preDate + "','mm-dd-yyyy'),";
				sql += "to_date('" + testDate + "','mm-dd-yyyy'),'";
				sql += diagnosis + "','" + description + "')";

				try {
					stmt = conn.createStatement();
					stmt.executeUpdate(sql);
				} catch (Exception ex) {
					out.println("<hr>" + ex.getMessage() + "<hr>");
				}

				try {
					sql = "drop index patient_name_idx";
					stmt.execute(sql);
					sql = "create index patient_name_idx on radiology_record(patient_name) indextype is ctxsys.context";
					stmt.execute(sql);
					sql = "drop index diagnosis_idx";
					stmt.execute(sql);
					sql = "create index diagnosis_idx on radiology_record(diagnosis) indextype is ctxsys.context";
					stmt.execute(sql);
					sql = "drop index description_idx";
					stmt.execute(sql);
					sql = "create index description_idx on radiology_record(description) indextype is ctxsys.context";
					stmt.execute(sql);
				} catch (Exception ex) {
					out.println("<hr>" + ex.getMessage() + "<hr>");
				}

				stmt.executeUpdate("commit");

				out.println("<form name=upload-image method=POST enctype=multipart/form-data action=ImageUploader>");
				out.println("<table id='form_table'><tr><th>File path: </th><td><input name=file-path type=file size=30></input></td></tr>");
				out.println("<tr><td ALIGN=CENTER COLSPAN=2><input type=submit name=pSubmit value=Upload></td></tr>");
				out.println("<input type=hidden name=recordid value="
						+ record_id + ">");
				out.println("</table></form>");
				
				manager.closeConnection();

			} else if (request.getParameter("pSubmit") != null) {
				int record_id = Integer.parseInt(request.getParameter(
						"recordid").toString());

				//From one picture upload to the next
				out.println("<form name=upload-image method=POST enctype=multipart/form-data action=ImageUploader>");
				out.println("<table id='form_table'><tr><th>File path: </th><td><input name=file-path type=file size=30></input></td></tr>");
				out.println("<tr><td ALIGN=CENTER COLSPAN=2><input type=submit name=pSubmit value=Upload></td></tr>");
				out.println("<input type=hidden name=recordid value="
						+ record_id + ">");
				out.println("</table></form>");

			} else {
				//default page
				out.println("<form method=post action=upload.jsp><table id='form_table'>");
				out.println("<tr><th>Patient Name:</th> <td><input type=text name=patientName maxlength=24></td></tr>");
				out.println("<tr><th>Doctor Name:</th> <td><input type=text name=doctorName maxlength=24></td></hr>");
				out.println("<tr><th>Test Type:</th> <td> <input type=text name=test maxlength=24></td></hr>");
				out.println("<tr><th>Prescribing Date<br/> (mm-dd-yyyy):</th> <td> <input type=text name=pdate></td></hr>");
				out.println("<tr><th>Test Date<br/> (mm-dd-yyyy):</th> <td> <input type=text name=tdate></td></hr>");
				out.println("<tr><th>Diagnosis:</th> <td> <textarea name=diagnosis cols=23 rows=4 maxlength=128></textarea></td></hr>");
				out.println("<tr><th>Description:</th> <td> <textarea name=description cols=23 rows=6 maxlength=1024></textarea></td></hr>");
				out.println("<tr><td align=right colspan=2><input type=submit name=bSubmit value=Submit></td></tr>");
				out.println("</table></form>");
			}
		}
	%>

</body>
</html>